package com.kong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kong.constant.MessageConstant;
import com.kong.entity.Result;
import com.kong.service.MemberService;
import com.kong.service.OrderService;
import com.kong.service.ReportService;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;


/**
 * @author yang2013
 * @data 2020/10/18 18:34
 */
@RestController
//@SuppressWarnings("deprecation")
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;
    @Reference
    private ReportService reportService;
    /**
     * 获取一面内每个月会员数量
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        try {
            Map memberReport=memberService.getMemberReport();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberReport);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map map=orderService.getSetmealReport();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    /**
     * 通过性别获取饼图
     */
    @GetMapping("/getMemberReportBySex")
    public Result getMemberReportSex(){
        try {
            Map memberReport=memberService.getMemberReportBySex();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberReport);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    /**
     * 通过年龄过去饼图
     */
    @GetMapping("/getMemberReportByAge")
    public Result getMemberReportByAge(){
        try {
            Map memberReport=memberService.getMemberReportByAge();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberReport);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    /**
     * 通过指定日期段查看会员数量的折线图
     */
    @PostMapping("/getMemberReportByFixedDate")
    public Result getMemberReportByFixedDate(String startDate,String endDate){
        try {
            Map memberReport=memberService.getMemberReportByDate(startDate,endDate);
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,memberReport);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    /**
     * 运营数据统计
     */
    @GetMapping(value = "/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map map = reportService.exportBusinessReport();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
        try {
            //获取模板所需要的数据
            Map map=reportService.exportBusinessReport();
            String templateName=request.getSession().getServletContext().getRealPath("template")+ File.separator+"report_template.xlsx";
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(new File(templateName)));
            XLSTransformer transformer = new XLSTransformer();
            //使用模板生成excel文件
            transformer.transformWorkbook(xssfWorkbook,map);
            //获得输出流，需要使用response的输出流，这样能和request对应
            OutputStream outputStream=response.getOutputStream();
            //设置头部信息
            response.setHeader("content-Disposition","attachment;filename=abc.xlsx");
            //设置文件类型  可以再tomcat的web.xml中查找名称
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            xssfWorkbook.write(outputStream);
            xssfWorkbook.close();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }


    /**
     * 使用pdf导出预约的数据
     *技术点 jasper
     */
    @GetMapping("/exportBusinessReportPDF")
    public Result exportBusinessReportPDF(HttpServletRequest request,HttpServletResponse response){
        try {
            //获取数据
            Map map = reportService.exportBusinessReport();

            //获取需要遍历填充的JavaBean
            List<Map> mapList= (List<Map>) map.get("hotSetmeal");

            //获取模板
            String jrxml = request.getSession().getServletContext().getRealPath("template")+File.separator+"exportBusinessTemplate.jrxml";
            String jasper = request.getSession().getServletContext().getRealPath("template")+File.separator+"exportBusinessTemplate.jasper";
            //编译为jasper文件
            JasperCompileManager.compileReportToFile(jrxml,jasper);

            //填充数据
            JasperPrint jasperPrint=JasperFillManager.fillReport(jasper,map,new JRBeanCollectionDataSource(mapList));
            OutputStream outputStream=response.getOutputStream();

            //为response设置类型
            response.setContentType("application/pdf");
            //设置文件名称
            response.setHeader("content-Disposition","attachment;filename=bus.pdf");
            //输出pdf文件
            JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);
            outputStream.flush();
            outputStream.close();

            return null;
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
